<?php
namespace app\admin\model;

use think\Db;
class ApiProjectModel extends CommonModel
 {
  	private static $model;
	
	public function __construct()
	{
		self::$model = Db::name ('api_project');
	}

     /**
      * func  更新 or 添加
      */
     public function addOrUpdateApiProjectInfo($data = [], $id = 0)
     {
         if (!$data) {
             return false;
         }
         if ($id) {
             $data = array_merge($data, ['update_time' => time()]);
             if (self::$model->where(array('id' => $id))->update($data)) {
                 return $id;
             } else {
                 return false;
             }
         } else {
             $data = array_merge($data, ['create_time' => time(), 'update_time' => time(), 'uuid' => uuid()]);
             if (self::$model->insert($data)) {
                 return self::$model->getLastInsID();
             } else {
                 return false;
             }
         }
     }

     /**
      * func 列表
      */
     public function getApiProjectListInfo($condition = [], $orderArr = [], $pageArr = [], $fieldsArr = [], $joinArr = [])
     {
         return self::$model->alias('a')
             ->field($fieldsArr)
             ->where($condition)
             ->join($joinArr)
             ->order($orderArr)
             ->paginate($pageArr['limit'], false, ['page' => $pageArr['page']]);
     }

     /**
      * func 单条
      */
     public function getApiProjectOneInfo($condition = [], $orderArr = [], $fieldsArr = [], $joinArr = [])
     {
         return self::$model->alias('a')
             ->field($fieldsArr)
             ->where($condition)
             ->join($joinArr)
             ->order($orderArr)
             ->find();
     }

     /**
      * func 总数
      */
     public function getApiProjectCount($condition = [],$joinArr = [])
     {
         return (int)self::$model->alias('a')
             ->join($joinArr)
             ->where($condition)
             ->count();
     }

     /**
      * func 求和
      */
     public function getApiProjectSum($condition = [], $field = null)
     {
         return (float)self::$model->alias('a')
             ->where($condition)
             ->sum($field);
     }

     /**
      * func 更新项目上了
      */
     public function triggerProjectUpdateData($projectId = 0)
     {
         $projectInfo = Db::name('api_project')
             ->field(['id', 'spot_id', 'project_date', 'status'])
             ->where('id', '=', $projectId)
             ->find();
         if ($projectInfo && $projectInfo['status'] == 1)
         {
             // 已加入人数
             $joinCount = Db::name('api_resume')
                 ->where('project_id', '=', $projectInfo['id'])
                 ->where('status', '=', 1)
                 ->count();
             Db::name('api_project')
                 ->where('id', '=', $projectInfo['id'])
                 ->update([
                     'project_join_num' => $joinCount,
                 ]);
         }
         return 1;
     }

    /**
     * func 获取账号管理的项目id
     */
    public function getProjectIdArr(array $status = [])
    {
        if (session('admin_is_operate') == 1) {
            $condition['id'] = ['gt', 0];
        } else {
            $condition['user_id'] = ['eq', session('ADMIN_ID')];
        }
        if ($status) {
            $condition['status'] = ['in', $status];
        }
        return (array)self::$model->where($condition)->column('id');
    }


    /**
     * func 获取项目列表
     */
    public function lastProjectListInfo(array $params = [])
    {
        return self::$model->field(['uuid','project_title','project_total_num','project_join_num'])
            ->where('spot_id', $params['spot_id'])
            ->where('project_date', $params['date'])
            ->where('project_join_num < project_total_num')
            ->where('status', 1)
            ->order('sort DESC, id ASC')
            ->limit(20)
            ->select();
    }

    /**
     * func 获取最近的一个项目
     */
    public function lastProjectOneInfo(array $params = [])
    {
        if (!intval($params['spot_id']) || !trim($params['date']))
        {
            return ['status' => false, 'message' => '参数错误'];
        }
        $result = self::$model
            ->where('spot_id', $params['spot_id'])
            ->where('project_date', $params['date'])
            ->where('project_join_num < project_total_num')
            ->where('status', 1)
            ->order('sort DESC, id ASC')
            ->find();

        return $result;
    }

 }